<?php
$this->useClass("Action_Community_FeedHandler");
class Action_Community_ShowForum extends Action_Community_FeedHandler
{
	var $ajax_functions = array(
		'ajax_getSomePosts',
		);

	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderForum");

		return new AggregatedModel_Feed_ReaderForum();
	}

	public function getWidgetsConfig ()	{
		$widgets = array();

		$widgets[] = array (
			'name' => 'CreateBtnWidget',
			'params' => array(
				'widget_position' => 'createbtn',
				'widget_view' => 'WG_CreateBtnWidget',
			),
		);

		$widgets[] = array (
			'name' => 'Feed_DoctypesMenuWidget',
			'params' => array(
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_DoctypesMenuWidget',
			),
		);

		$widgets[] = array (
			'name' => 'TagsWidget',
			'params' => array(
				'community_oid' => $this->aa->acl_data['comity_oid'],
				'tdi_oid'=> $this->aa->acl_data['tdi_oid'],
				'tag' => $this->getParam('tag'),
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_TagsWidget',
			),
		);

		$widgets[] = array (
			'name' => 'RssWidget',
			'params' => array(
				'widget_view' => 'WG_RssWidget',
				'widget_position' => 'tags',
			),
		);

		return $widgets;
	} // end of function getWidgetsConfig


	protected function init() {
		parent::init();
		$this->data = $this->aa->acl_data;
		/*
		$this->rh->UseClass('RL_CyrDate');
		$this->acl_assistant = $this->module->aa;

		$this->data = $this->acl_assistant->getInfo();
		// HACK : 2008-01-18 : lucky {{{
		// если в разделе, смотрим операции выставленные разделом, иначе -- сообществом.
		$object_oid = isset($this->data['tdi_oid']) ? $this->data['tdi_oid'] : $this->data['comity_oid'];
		$this->acl_assistant->getAvailableOperations($object_oid);
		//$this->acl_assistant->getAvailableOperations();
		// }}}

		$this->ctx->mm->setAclAssistant( $this->acl_assistant );
		*/
		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids);

		/*
		$this->setAclAssistant($this->acl_assistant);
		$this->determineModelParams();
		*/
	}

	public function ajax_getSomePosts( $page_number ) {
		$this->setParam("page_number", $page_number);

		$aa = &$this->aa;
		$aa->checkDeletions(array($this, "shutdown") );

		$this->model->doStuff();
		$view = $this->rh->views->findByName('forum_items');
		$view->data = $aa->template_data;
		$view->data['forum_topic_list'] = $this->model->getItems();
		$view->data['forum_topic_sort'] = $this->getParam('order_by');

		$items_node = &$this->rh->ctx->cp->add_node("items");
		$items_node->set_data( $view->getContent() ); // рендеринг итемов
		$this->addPagerNodes( &$this->rh->ctx->cp );
		/*
		$order_by = $this->order_by;
		$this->items = $this->fetch_posts( $order_by, $page_number ); // структура итемов

		$view = $this->rh->views->findByName('forum_items');
		$view->data['forum_topic_list'] = $this->items;
		$view->data['forum_topic_sort'] = $order_by;

		$items_node = &$this->rh->ctx->cp->add_node("items");
		$items_node->set_data( $view->getContent() ); // рендеринг итемов

		$ispagecount_node = &$this->rh->ctx->cp->add_node("ispagecount"); // есть ли следующая страница?
		$pages_count = $this->prepare_pages_count( $q );

		if ($page_number < $pages_count) {
			$ispagecount_node->set_data( 1 ); // есть
		} else $ispagecount_node->set_data( 0 );


		$itemsremaining_node = &$this->rh->ctx->cp->add_node("itemsremaining");

		$items_out = $this->page_citems * $page_number; // кол-во итемов отправленных юзеру вообще
		$items_remain = $this->items_count - $items_out;

		if ( $items_remain >= $this->page_citems ) {
			$itemsremaining_node->set_data( $this->page_citems );
		} else $itemsremaining_node->set_data( $items_remain );


		$this->shutdown();
		*/
	}
	public function process ()
	{
		$aa = &$this->aa;
		$aa->checkDeletions(array($this, "shutdown") );

		$view = $this->rh->views->findByName('forum');
		$view->data = $aa->template_data;

		$view->data['can_create'] = $aa->is_granted(array('item_create', 'item_create_premod'));

		$this->model->doStuff();
		$items = $this->model->getItems();
		$pager = $this->getPagerInfo();

		$view->data['forum_topic_list'] = $items;
		$view->data['forum_topic_sort'] = $this->getParam('order_by');
		$view->data['page'] = $pager;
		$view->data['items_remaining'] = $pager['items_remaining'];

		$this->renderFilters($view);

		$view->data['__widgets__'] = $this->widgets;

		$this->rh->UseClass("RL_URLGenerator");
		$ug = new RL_URLGenerator($this->rh->router->getLastMatched()); //$this->rh->router->getRule('object_undelete')
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		if (isset($pager['prev'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['prev'])));
			$view->data["prev_url"] = $ug->generateUrl();
		}

		if (isset($pager['next'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['next'])));
			$view->data["next_url"] = $ug->generateUrl();
		}

		unset($ug);

		// для постраничной листалки ajax handler
		if ( isset($this->params['tdi_url']) ) {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showforum_tdi_ajax'));
		} else {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showforum_ajax'));
		}
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);
		$ug->setGet($this->rh->GET);

		$view->data['ajax_url'] = $ug->generateUrl();

		$this->rh->response->setContent( $view );

		// -- touch visitor stat -- {{{
		// visit  tdi
		$this->ctx->mm->getModel("Visitable", "m")->getVisitable('Tdi')->visit();
		//
		// -- touch visitor stat -- }}}

		$this->shutdown();

	} // end of function execute

}
